import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Bella on 13/04/2014.
 */
public class InvertedIndex {
    /** word, url, wordScore**/
     private TreeMap<String, TreeMap<String, Double>> wordList;

    public InvertedIndex() {
        wordList= new TreeMap<String, TreeMap<String, Double>>();
    }

    /**
     * this function gets a new word and adds it to the word list.
     * @param word
     * @param url
     * @param wordCounter
     * @param pageLength
     */
    public void addWord(String word, String url,int wordCounter, int pageLength) {
        //if word and url already exist
        if (wordList.containsKey(word) && wordList.get(word).containsKey(url)) {
            //System.out.println("word and url already exist");
            //if word already exist but url is not
        } else if (wordList.containsKey(word)) {
            TreeMap<String, Double> urlsAndScores = wordList.get(word);
            if (urlsAndScores == null) {
                System.err.println("error: word already exists but info is null");
            }
            urlsAndScores.put(url, (new Double(wordCounter) / new Double(pageLength)));
        } else {
            TreeMap<String, Double> urlsAndScores = new TreeMap<String, Double>();
            urlsAndScores.put(url, (new Double(wordCounter) / new Double(pageLength)));
            wordList.put(word, urlsAndScores);
        }
    }

        public Map<String, Double> getUrlAndScoreForWord(String word) {
            return wordList.get(word);
        }



    }
